/*函数 */

//函数表达式
function hello(name:string):void{
  console.log('hello',name);
}
hello('ggg');

type userFunction = (a:string,b:string) => string; //非箭头函数 ，而是一种分隔符
let getUserName:userFunction = function(firstName:string,lastName:string){
  return firstName + lastName;
}

//可选参数 必须是最后一个
function print(name:string,age?:number){
  console.log(name,age);
}

//默认参数 
function ajax(url:string,method:string='GET'){
 console.log(url,method);
}
ajax('/users');

//剩余参数
function sum(prefix:string,...numbers:number[]){
  return prefix + numbers.reduce((acc,cur)=>acc+cur,0);
}
console.log(sum('$',1,2,3));

//函数的重载
function attr(val:number | string){
  let obj:any = {};
  if(typeof val === 'string'){
    obj.name = val;
  }else if(typeof val === 'number'){
    obj.age = val;
  }
}
attr('gugu');

function sum2(a:string,b:string):any;
function sum2(a:number,b:number):any; //函数声明or签名
function sum2(a: string | number,b: string | number){

}
sum2(1,3);
sum2(1,'b');//限定了a和b的类型值是一样的
